home *** CD-ROM | disk | FTP | other *** search
Text File | 1988-01-09 | 9.0 KB | 379 lines | [TEXT/PJMM] |
- UNIT solve;
-
- INTERFACE
-
- USES
- ROM85, PrintTraps, PlotGlobals, Misc;
-
- PROCEDURE quad (a, b, c : real;
- VAR x1, x2 : real;
- VAR result : integer);
-
- FUNCTION solveit : integer;
- PROCEDURE doPlot;
- PROCEDURE PrQDStuff (pRect : rect;
- QDdevice : integer);
-
- IMPLEMENTATION
-
- FUNCTION positivecalc (a, b, check : real) : real;
- BEGIN
- positivecalc := (-b + sqrt(check)) / (2 * a);
- END;
-
- FUNCTION negativecalc (a, b, check : real) : real;
- BEGIN
- negativecalc := (-b - sqrt(check)) / (2 * a);
- END;
-
- PROCEDURE doDialog;
- VAR
- dialogP : DialogPtr;
- item : integer;
- dtype : integer;
- ditem : handle;
- drect : rect;
- dtext : Str255;
- BEGIN
- dialogP := GetNewDialog(ParamDialog, NIL, pointer(-1));
- IF dialogP = NIL THEN
- BEGIN
- doMessage('Dialog crash!', 'We are dead...', '', '');
- ExitToShell;
- END;
- initCursor;
- IF dialogflg THEN
- BEGIN
- dtext := StringOf(a : 4 : 1);
- GetDItem(dialogP, dA, dtype, ditem, drect);
- SetIText(ditem, dtext);
- dtext := StringOf(b : 4 : 1);
- GetDItem(dialogP, dB, dtype, ditem, drect);
- SetIText(ditem, dtext);
- dtext := StringOf(c : 4 : 1);
- GetDItem(dialogP, dC, dtype, ditem, drect);
- SetIText(ditem, dtext);
- dtext := StringOf(step : 5 : 3);
- GetDItem(dialogP, dSTEP, dtype, ditem, drect);
- SetIText(ditem, dtext);
- dtext := StringOf(xscale);
- GetDItem(dialogP, dXSCALE, dtype, ditem, drect);
- SetIText(ditem, dtext);
- dtext := StringOf(yscale);
- GetDItem(dialogP, dYSCALE, dtype, ditem, drect);
- SetIText(ditem, dtext);
- END;
- REPEAT
- ModalDialog(NIL, item);
- UNTIL item = dOK;
- GetDItem(dialogP, dA, dtype, ditem, drect);
- GetIText(ditem, dtext);
- ReadString(dtext, a);
- GetDItem(dialogP, dB, dtype, ditem, drect);
- GetIText(ditem, dtext);
- ReadString(dtext, b);
- GetDItem(dialogP, dC, dtype, ditem, drect);
- GetIText(ditem, dtext);
- ReadString(dtext, c);
- GetDItem(dialogP, dSTEP, dtype, ditem, drect);
- GetIText(ditem, dtext);
- ReadString(dtext, step);
- GetDItem(dialogP, dXSCALE, dtype, ditem, drect);
- GetIText(ditem, dtext);
- ReadString(dtext, xscale);
- GetDItem(dialogP, dYSCALE, dtype, ditem, drect);
- GetIText(ditem, dtext);
- ReadString(dtext, yscale);
- PlotDocHandle^^.aParam := a;
- PlotDocHandle^^.bParam := b;
- PlotDocHandle^^.cParam := c;
- PlotDocHandle^^.stepParam := step;
- PlotDocHandle^^.xParam := xscale;
- PlotDocHandle^^.yParam := yscale;
- dialogflg := true;
- DisposDialog(dialogP);
- END;
-
- PROCEDURE quad; {(a, b, c : real;var x1, x2 : real;var result : integer);}
- VAR
- check : real;
- BEGIN
- result := 0;
- check := (b * b) - (4 * a * c);
- IF result = 0 THEN
- BEGIN
- { Check if double root exists }
- IF check = 0 THEN
- BEGIN
- result := 2;
- x1 := positivecalc(a, b, check);
- x2 := x1;
- END;
- { Check if real result}
- IF check > 0 THEN
- BEGIN
- result := 1;
- x1 := positivecalc(a, b, check);
- x2 := negativecalc(a, b, check);
- END;
- { Check if root is complex }
- IF check < 0 THEN
- BEGIN
- result := 3;
- check := -check;
- x1 := positivecalc(a, b, check);
- x2 := negativecalc(a, b, check);
- END;
- END;
- END;
-
- PROCEDURE PrQDStuff; {(pRect : rect; QDdevice : integer);}
- CONST
- Display = 1;
- LaserWriter = 2;
- ImageWriter = 3;
- NoJust = 0;
- LeftJust = 1;
- CenterJust = 2;
- RightJust = 3;
- FullJust = 4;
- LinesInParagraph = 5;
- {selected MacDraw comments}
- picDwgBeg = 130;
- picDwgEnd = 131;
- picGrpBeg = 140;
- picGrpEnd = 141;
- TextBegin = 150;
- TextEnd = 151;
- StringBegin = 151;
- StringEnd = 153;
- TextCenter = 154;
- {postscript comments}
- SetLineWidth = 182;
- PostScriptBegin = 190;
- TextIsPostscript = 194;
- PostScriptEnd = 191;
- TYPE
- widhdl = ^widptr;
- widptr = ^widpt;
- widpt = Point;
-
- TTxtPicRec = PACKED RECORD
- tJus : Byte;
- tFlip : Byte;
- tRot : Integer;
- tLine : Byte;
- tCmnt : Byte;
- END;
-
- VAR
- le, tp, ri, bo : integer;
- str1, str2, str3, str4, str5 : str255;
- str6, str7, str8, str9 : str255;
- hPos, vPos, hor, ver : integer;
- x, y, z1, z2 : real;
- rBox, ClipBox : rect;
- Width : Widhdl;
- leading : integer;
- LineNo : integer;
- ParagraphBegin : Point;
- Indent : integer;
- Paragraph : ARRAY[1..LinesInParagraph] OF str255;
- TxtPicRec : TTxtPicRec;
- TxtPicPtr : QDPtr;
- TxtPicHdl : QDHandle;
- TextClipRgn : RgnHandle;
- SaveClip : RgnHandle;
- fInfo : FontInfo;
- BEGIN
- SaveClip := NewRgn;
- GetClip(SaveClip);
- ClipRect(pRect);
- TextClipRgn := NewRgn;
- penNormal;
- IF QDdevice = LaserWriter THEN
- BEGIN
- TextFont(times);
- TextSize(10);
- TextFace([]);
- TextMode(srcOr);
- END;
- hor := (pRect.right - pRect.left) DIV 2;
- ver := (pRect.bottom - pRect.top) DIV 2;
- Width := Widhdl(NewHandle(sizeof(widpt)));
- Width^^.h := 10;
- Width^^.v := 1;
- TxtPicPtr := @TxtPicRec;
- TxtPicHdl := @TxtPicPtr;
- TxtPicRec.tJus := LeftJust;
- TxtPicRec.tFlip := 0; {no flip}
- TxtPicRec.tRot := 0; {no rotation}
- TxtPicRec.tLine := 2; {1 1/2 spacing}
- GetFontInfo(fInfo);
- leading := fInfo.descent + fInfo.ascent + fInfo.leading;
- Indent := 2;
- x := -xscale / 2;
- y := a * x * x + (b * x) + c;
- hPos := integer(round(x * hor * 2 / xscale + hor));
- vPos := integer(round(-y * ver * 2 / yscale + ver));
- z1 := -b / (2 * a);
- z2 := (4 * a * c - (b * b)) / (4 * a);
- le := 2;
- tp := ver + (ver DIV 3);
- ri := 140;
- IF ri >= (hor + hor DIV 3) THEN
- ri := hor + hor DIV 3;
- bo := ver + ver - 2;
- setRect(rBox, le, tp - 14, ri, bo);
- ParagraphBegin.h := 4;
- ParagraphBegin.v := tp;
-
- {Graph Text}
- str1 := stringOf(-xscale DIV 2);
- str2 := stringOf(yscale DIV 2);
- str3 := stringOf(xscale DIV 2);
- str4 := stringOf(-yscale DIV 2);
-
- Paragraph[1] := StringOf('y=ax^2 + bx + c', chr(13));
- Paragraph[2] := StringOf('a=', a : 3 : 1, ', b=', b : 3 : 1, ', c=', c : 3 : 1, chr(13));
- Paragraph[3] := StringOf('x1=', x1 : 5 : 3, ', x2=', x2 : 5 : 3, chr(13));
- CASE result OF
- 1 :
- Paragraph[4] := StringOf('Two Real Roots, x1, x2', chr(13));
- 2 :
- Paragraph[4] := StringOf('Double Root', chr(13));
- 3 :
- Paragraph[4] := StringOf('Two Complex Roots ', chr(13));
- OTHERWISE
- ;
- END;
- Paragraph[5] := StringOf('Slope 0 = (', z1 : 2 : 1, ',', z2 : 2 : 1, ')', chr(13));
-
- PenNormal;
- BackColor(Color[BackgroundColor]);
- ForeColor(Color[AxisColor]);
-
- {Drawing Boundry}
- PicComment(picDwgBeg, 0, NIL); {Begin MacDraw Document}
- PicComment(picGrpBeg, 0, NIL);
- PicComment(SetLineWidth, 2, Handle(Width));
- IF QDdevice = Display THEN
- FillRect(pRect, white);
- FrameRect(pRect);
-
- {Two Axis}
- PicComment(picGrpBeg, 0, NIL);
- moveto(0, ver);
- line(hor + hor, 0);
- moveto(hor, 0);
- line(0, ver + ver);
- PicComment(picGrpEnd, 0, NIL);
-
- ForeColor(Color[GraphColor]);
-
- {Plot Itsef}
- PicComment(picGrpBeg, 0, NIL);
- moveto(hPos, vPos);
- REPEAT
- x := x + step;
- y := a * x * x + (b * x) + c;
- hPos := integer(round(x * hor * 2 / xscale + hor));
- vPos := integer(round(-y * ver * 2 / yscale + ver));
- WITH pRect DO
- IF (hPos < right) AND (hPos > left) AND (vPos < bottom) AND (vPos > top) THEN
- LineTo(hPos, vPos)
- ELSE
- moveto(hPos, vPos);
- UNTIL x >= xscale / 2;
- PicComment(picGrpEnd, 0, NIL);
-
- ForeColor(Color[1]);
-
- {Axis Text}
- moveto(4, ver + 14);
- DrawString(str1);
- moveto(hor - 40, 14);
- DrawString(str2);
- moveto(hor + hor - 50, ver + 14);
- DrawString(str3);
- moveto(hor - 40, ver + ver - 14);
- DrawString(str4);
-
- {Box }
- PicComment(picGrpBeg, 0, NIL);
- PicComment(picGrpBeg, 0, NIL);
- PicComment(SetLineWidth, 2, Handle(Width));
- IF QDdevice = Display THEN
- fillRect(rBox, white);
- frameRect(rBox);
- PicComment(picGrpEnd, 0, NIL); {of box}
-
- GetClip(TextClipRgn);
- ClipBox := rBox;
- ClipRect(ClipBox);
-
- {Box Text}
- PicComment(TextBegin, sizeof(TTxtPicRec), Handle(TxtPicHdl));
- FOR LineNo := 1 TO LinesInParagraph DO
- BEGIN
- moveto(ParagraphBegin.h, ParagraphBegin.v);
- move(Indent, (LineNo - 1) * leading);
- DrawString(Paragraph[LineNo]);
- END;
- PicComment(TextEnd, 0, NIL);
- PicComment(PicGrpEnd, 0, NIL); {of Box & text}
- PicComment(PicGrpEnd, 0, NIL); {of select all objects}
- picComment(picDwgEnd, 0, NIL); {of drawing}
-
- SetClip(SaveClip);
- disposHandle(handle(width));
- DisposeRgn(TextClipRgn);
- DisposeRgn(SaveClip);
- END;
-
- PROCEDURE PlotMe;
- CONST
- Display = 1;
- VAR
- Displayrect : rect;
- pstate : PenState;
- BEGIN
- Displayrect := PicRect;
- IF PlotDocHandle^^.drawing <> NIL THEN
- BEGIN
- KillPicture(DrawingPic);
- PlotDocHandle^^.drawing := NIL;
- END;
- GetPenState(pstate);
- DrawingPic := OpenPicture(Displayrect);
- PrQDStuff(Displayrect, Display);
- ClosePicture;
- SetPenState(pstate);
- InvalRect(Displayrect); {draw picture}
- PlotDocHandle^^.drawing := DrawingPic; {save it}
- END;
-
- FUNCTION solveit; { : integer;}
- BEGIN
- doDialog;
- IF a <> 0 THEN
- quad(a, b, c, x1, x2, result)
- ELSE
- result := -1;
- solveit := result;
- END;
-
- PROCEDURE doPlot;
- BEGIN
- result := solveit;
- showWindow(PlotWindow);
- IF PlotWindow <> FrontWindow THEN
- SelectWindow(PlotWindow);
- IF result <> -1 THEN
- BEGIN
- PlotMe;
- EnableItem(myMenus[FileM], fPrint);
- END;
- END;
-
- END.